// 所有关于课程模块的vuex 设计
import { getCourseApi } from '../../api/course'
import { getDirVideoApi } from '../../api/course'
const courseStore = {
  namespaced: true,
  state: {
    courseList: [],//课程列表数据
    pageStart: 1,//当前页码
    pageSize: 0,//一页的条数
    total: 0,//数据总条数
    endPage: 0,//末页
    videoFlag: false,//课程视频添加弹出框
    videoList: [],//目录下的视频列表数据
  },
  mutations: {
    changeData (state, res) {
      if (res.err) {
        state.courseList = []
      } else {
        state.courseList = res.data.list;
        state.total = res.data.count; //总数据条数
        state.pageSize = res.data.pageSize;
        state.endPage = Math.ceil(res.data.count / res.data.pageSize)
      }
    },
    changePage (state, page) { //改变当前页码
      state.pageStart = page;
    },
    changevideoFlag (state, flag) {//改变添加视频弹出层
      state.videoFlag = flag;
    },
    changeDirVideoList (state, res) { //赋值请求回来的目录下的视频
      state.videoList = res.data.list;
    }
  },
  actions: {
    async getCourseAction ({ commit }, data) { //获取课程列表的actions
      var res = await getCourseApi(data);  //请求课程的接口
      console.log(res)
      commit("changeData", res)
    },
    async getDirVideoAction ({ commit }, data) { //获取目录下对应视频的actions
      var res = await getDirVideoApi({
        _id: data[0],
        CDid: data[1]
      })
      commit("changeDirVideoList", res)
    }
  },
  getters: {
    courseList: state => state.courseList,
    pageStart: state => state.pageStart,
    pageSize: state => state.pageSize,
    total: state => state.total,
    endPage: state => state.endPage,
    videoFlag: state => state.videoFlag,
    videoList: state => state.videoList
  }
}
export default courseStore